package com.study.javaBasic.classloader;

import java.io.*;

/**
 * @Name: CustomClassLoader
 * @Description: java类作用描述
 * @Author: yn
 * @CreateDate: 2023/2/13 16:21
 * @Remark: 特殊说明
 * @Version: 1.0
 */
public class CustomClassLoader extends ClassLoader{
	@Override
	protected Class<?> findClass(String name) {
		String classPath = "C:\\Users\\yn\\Desktop\\dpm_运行环境\\application\\wonder-dpm\\BOOT-INF\\classes\\"+name.replace(".", "/")+".class";
		InputStream inputStream = null;
		ByteArrayOutputStream outputStream = null;
		try {
			inputStream = new FileInputStream(classPath);
			outputStream = new ByteArrayOutputStream();
			int temp = 0;
			while((temp = inputStream.read()) != -1){
				outputStream.write(temp);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				outputStream.close();
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		byte[] bytes = outputStream.toByteArray();
		return defineClass(name,bytes,0,bytes.length);
	}
}
